package com.ruoyi.flowable.listener.asset;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.equ.domain.EquStockin;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;

/**
 * 验收3、数量配件验收、技术资料验收
 */
@Slf4j
@Component("assetListener_3")
public class AssetListener_3 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //数量配件验收:应到数量+实到数量+规格核对结果+备注，如“1+1+一致+无缺失”
            List<Map<String, Object>> equipments = getElements(execution, "equipmentForm");
            List<Map<String, Object>> attachments = getElements(execution, "attachmentForm");
            List<Map<String, Object>> tools = getElements(execution, "toolForm");

            HashMap<String, List<Map<String, Object>>> quantityCheckMap = new HashMap<>();
            quantityCheckMap.put("主机设备", equipments);
            quantityCheckMap.put("配套附件", attachments);
            quantityCheckMap.put("随机工具", tools);
            String quantityCheck = objectMapper.writeValueAsString(quantityCheckMap);

            //技术资料验收:合格证状态+说明书类型+检测报告状态+第三方报告路径<大型设备>
            //产品合格证
            String have1 = execution.getVariable("have1", String.class);
            String remark3_2_1 = execution.getVariable("remark3_2_1", String.class);
            HashMap<String, Object> have1Map = new HashMap<>();
            have1Map.put("有/无", have1);
            have1Map.put("备注", remark3_2_1);
            //产品说明书
            String have2 = execution.getVariable("have2", String.class);
            List<String> fileupload3_2_2 = getFileList(execution, "fileupload3_2_2");
            HashMap<String, Object> have2Map = new HashMap<>();
            have2Map.put("有/无", have2);
            have2Map.put("附件", fileupload3_2_2);
            //检测报告
            String have3 = execution.getVariable("have3", String.class);
            List<String> reportFile = getFileList(execution, "reportFile3_1_1");
            HashMap<String, Object> have3Map = new HashMap<>();
            have3Map.put("有/无", have3);
            have3Map.put("报告", reportFile);
            //保修卡
            String have4 = execution.getVariable("have4", String.class);
            String range3_2_4 = execution.getVariable("range3_2_4", String.class);
            HashMap<String, Object> have4Map = new HashMap<>();
            have4Map.put("有/无", have4);
            have4Map.put("保修范围", range3_2_4);
            //赋值
            HashMap<String, Object> docCheckMap = new HashMap<>();
            docCheckMap.put("产品合格证", have1Map);
            docCheckMap.put("产品说明书", have2Map);
            docCheckMap.put("检测报告", have3Map);
            docCheckMap.put("保修卡", have4Map);
            String docCheck = objectMapper.writeValueAsString(docCheckMap);

            EquStockin equStockin = execution.getVariable("equStockin", EquStockin.class);

            //附件链接
            String oldFileUrls = equStockin.getFileUrls();
            // 将 JSON 字符串解析为 Map<String, Object>
            Map<String, Object> fileUrlsMap = objectMapper.readValue(oldFileUrls, new TypeReference<Map<String, Object>>() {
            });
            fileUrlsMap.put("报告", reportFile);
            fileUrlsMap.put("附件", fileupload3_2_2);
            String fileUrls = objectMapper.writeValueAsString(fileUrlsMap);

            //审批记录
            List<Map<String, String>> approvalRecordList = getApprovalRecord(equStockin.getApprovalRecord());
            String approvalRecord = objectMapper.writeValueAsString(approvalRecordList);

            //审批信息
            String approvalInfo = equStockin.getApprovalInfo();
            Map<String, Object> approvalInfoMap = objectMapper.readValue(
                    approvalInfo,
                    new TypeReference<Map<String, Object>>() {
                    });
            approvalInfoMap.put("数量配件验收", quantityCheckMap);
            approvalInfoMap.put("技术资料验收", docCheckMap);
            approvalInfoMap.put("附件链接", fileUrlsMap);
            approvalInfoMap.put("审批记录", approvalRecordList);
            approvalInfo = objectMapper.writeValueAsString(approvalInfoMap);

            equStockin.setQuantityCheck(quantityCheck);
            equStockin.setDocCheck(docCheck);
            equStockin.setFileUrls(fileUrls);
            equStockin.setApprovalRecord(approvalRecord);

            equStockin.setApprovalInfo(approvalInfo);

            execution.setVariable("equStockin", equStockin);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    //应到、实到封装
    private static List<Map<String, Object>> getElements(DelegateExecution execution, String varName) {
        List<Map<String, Object>> dataForm = (List<Map<String, Object>>) execution.getVariable(varName);
        List<Map<String, Object>> dataList = new ArrayList<>();
        dataForm.forEach(item -> {
            Map<String, Object> data = new HashMap<>();
            Integer yingnum = (Integer) item.get("yingnum");
            Integer shinum = (Integer) item.get("shinum");
            String flagStr = "不一致";
            if (yingnum.equals(shinum)) {
                flagStr = "一致";
            }
            data.put("明细", item.get("detail"));
            data.put("应到数量", yingnum);
            data.put("实到数量", shinum);
            data.put("数量是否一致", flagStr);
            data.put("型号规格", item.get("xinghao"));
            data.put("备注", item.get("remark"));
            dataList.add(data);
        });
        return dataList;
    }

    //审批记录
    public static List<Map<String, String>> getApprovalRecord(String approvalRecord) throws JsonProcessingException {
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        String deptName = SecurityUtils.getLoginUser().getUser().getDept().getDeptName();
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        HashMap<String, String> record = new HashMap<>();
        record.put("name", nickName);
        record.put("dept", deptName);
        record.put("date", date);

        List<Map<String, String>> approvalRecordList = objectMapper.readValue(approvalRecord, new TypeReference<List<Map<String, String>>>() {
        });
        approvalRecordList.add(record);
        return approvalRecordList;
    }
}
